<?php
class AdminController extends AppController {
    public $helpers = array('Html', 'Form', 'Navigation');
    var $components = array('CAS_Auth','Session', 'Navigation');
    //models to access
    var $uses = array('Manager','Employee','Hour','Project','Admin','Group','GroupMember','GroupManager','ProjectMember');
    public function beforeFilter(){
		$this->CAS_Auth->checkAuthentication();
		$this->account = $this->CAS_Auth->getGTAccount();
		$this->set('gtAccount', $this->account);

		//use authentication to display appropriate menu
		$this->permissions = array();
		if($this->Employee->find('count', array('conditions'=> array('Employee.name' => $this->account))) > 0)
			$this->permissions[] = 'employee';
		if($this->Manager->find('count', array('conditions'=> array('Manager.name' => $this->account))) > 0)
			$this->permissions[] = 'manager';
		if($this->Admin->find('count', array('conditions'=> array('Admin.admin_name' => $this->account))) > 0)
			$this->permissions[] = 'admin';

		$this->set('navMenu', $this->Navigation->getTree($this->permissions));
    }
    
    /**
    * Sets account for the view. Allows the view to determine if the user is logged in as a valid admin
    * 
    **/
    function setUpAdmin()
    {
    	$count=$this->Admin->find('count', array('conditions'=> array('Admin.admin_name' => $this->account)));
        if ($count != 1)
        {
            $this->set('gtAccount',NULL);
        }
	$admin = $this->Admin->find('first', array('conditions'=>array('Admin.admin_name' => $this->account)));
    	$this->set('admin',$admin);

    }

    function index()
    {
        $this->setUpAdmin();
    }

    //all admins passed to view
    function admins(){
	$this->setUpAdmin();
	$this->set('admins',$this->Admin->Find('all', array('className'=>'Admin')));
    }


    //all employees passed to view
    function employees(){
	$this->setUpAdmin();
	$this->set('emps',$this->Employee->Find('all', array('className'=>'Employee')));
    }
    
    //all managers passed to view
    function managers(){
	$this->setUpAdmin();
	$this->set('managers',$this->Manager->Find('all', array('className'=>'Manager')));
    }


    //all groups passed to view    
    function groups(){
	$this->setUpAdmin();
	$this->set('groups',$this->Group->Find('all', array('className'=>'Group')));
    }
 
    //all projects passed to view
    function projects(){
	$this->setUpAdmin();
	$this->set('projects',$this->Project->Find('all', array('className'=>'Project')));
    }

    //creates a new entry in the admins table
    function addAdmin(){
	if ($this->request->is('post'))
        {
            $name = $this->request->data['name'];
            $adminId= ($this->request->data['id']);
            if ($this->Admin->Save(array('admin_name'=>$name,'admin_id'=>$adminId)))
            {
                $this->redirect(array('action' => 'admins'));
            }
            else
            {
                $this->flash('One or more fields were invalid',array('action'=> 'admins'));
            }
        } 
    }


    //creates a new employee in the employees table
    function addEmployee(){
	if ($this->request->is('post'))
        {
            $name = $this->request->data['name'];
            $empID= $this->request->data['id'];
            if ($this->Employee->Save(array('name'=>$name,'employee_id'=>$empID)))
            {
                $this->redirect(array('action' => 'employees'));
            }
	    else{
		$this->flash('One or more fields were invalid', array('action'=>'employees'));
	    }
        } 
    }
    
    //creates a new entry in the group_members table
    function addEmployeeToGroup(){
	if ($this->request->is('post'))
        {
	    $groupId = $this->request->data['groupId'];
	    $empId = $this->request->data['empId'];
            if ($this->GroupMember->Save(array('group_id'=>$groupId,'emp_id'=>$empId)))
            {
                $this->redirect(array('action' => 'manageGroup',$groupId));
            }
            else
            {
                $this->flash('An error occured. The employee was not added to the group!', array('action'=>'groups'));
            }
        } 
    }

    //creates a new entry in the project_members table

    function addEmployeeToProject(){
	if ($this->request->is('post'))
        {
	    $projId = $this->request->data['projId'];
	    $empId = $this->request->data['employeeId'];
            if ($this->ProjectMember->Save(array('project_id'=>$projId,'employee_id'=>$empId)))
            {
                $this->redirect(array('action' => 'manageProject',$projId));
            }
            else
            {
                $this->flash('An error occured. The employee was not added to the project!', array('action'=>'groups'));
            }
        } 
    }

    //creates a new entry in the groups table
    function addGroup(){
	if ($this->request->is('post'))
        {
            $name = $this->request->data['name'];
            $groupId= $this->request->data['id'];
	    $desc= $this->request->data['description'];
            if ($this->Group->Save(array('name'=>$name,'id'=>$groupId,'description'=>$desc)))
            {
                $this->redirect(array('action' => 'groups'));
            }
            else
            {
                $this->flash('One or more fields were invalid', array('action'=>'groups'));
            }
        } 
    }

    //creates a new entry in the managers table
    function addManager(){
	if ($this->request->is('post'))
        {
            $name = $this->request->data['name'];
            $managerId= $this->request->data['id'];
            if ($this->Manager->Save(array('name'=>$name,'manager_id'=>$managerId)))
            {
                $this->redirect(array('action' => 'managers'));
            }
            else
            {
                $this->flash('One or more fields were invalid', array('action'=>'managers'));
            }
        } 
    }

    //creates a new entry in the group_managers table
    function addManagerToGroup(){
	if ($this->request->is('post'))
        {
	    $groupId = $this->request->data['groupId'];
	    $managerId = $this->request->data['managerId'];
            if ($this->GroupManager->Save(array('group_id'=>$groupId,'mgr_id'=>$managerId)))
            {
                $this->redirect(array('action' => 'manageGroup',$groupId));
            }
            else
            {
                $this->flash('An error occured. The manager was not added to the group', array('action'=>'groups'));
            }
        } 
    }

    //creates a new entry in the projects table
    function addProject(){
	if ($this->request->is('post'))
        {
            $id = $this->request->data['id'];
            $desc= $this->request->data['description'];
            if ($this->Project->Save(array('project_id'=>$id,'description'=>$desc)))
            {
                $this->redirect(array('action' => 'projects'));
            }
            else
            {
                $this->flash('One or more fields were invalid', array('action'=>'projects'));
            }
        } 
    }
    

    //looks up existing data for one admin entry and passes it to the view for the user to edit
    function editAdmin($adminId=null){
	    if($adminId==null){
		$this->redirect(array('action'=>'admins'));
 	    }
	    $adminName = $this->Admin->find('first', array('conditions'=>array('Admin.admin_id'=> $adminId)));
	    //send data to fields in view
	    $this->set('name',$adminName['Admin']['admin_name']);
	    $this->set('adminId',$adminId);
    }

    //looks up existing data for one employee entry and passes it to the view for the user to edit
    function editEmployee($empId=null){
	    if($empId==null){
		$this->redirect(array('action'=>'employees'));
 	    }
	    $empName = $this->Employee->find('first', array('conditions'=>array('Employee.employee_id'=> $empId)));
	    //send data to fields in view
	    $this->set('name',$empName['Employee']['name']);
	    $this->set('empId',$empId);
    }

    //looks up existing data for one group entry and passes it to the view for the user to edit
    function editGroup($groupId=null){
	    if($groupId==null){
		$this->redirect(array('action'=>'groups'));
 	    }
	    $group = $this->Group->find('first', array('conditions'=>array('Group.id'=> $groupId)));
	    //send data to fields in view
	    $this->set('name',$group['Group']['name']);
	    $this->set('id',$group['Group']['id']);
	    $this->set('desc',$group['Group']['description']);
    }

    //looks up existing data for one manager entry and passes it to the view for the user to edit
    function editManager($managerId=null){
	    if($managerId==null){
		$this->redirect(array('action'=>'managers'));
 	    }
	    $managerName = $this->Manager->find('first', array('conditions'=>array('Manager.manager_id'=> $managerId)));
	    //send data to fields in view
	    $this->set('name',$managerName['Manager']['name']);
	    $this->set('managerId',$managerId);
    }

    //looks up existing data for one project entry and passes it to the view for the user to edit
    function editProject($projectId=null){
	    if($projectId==null){
		$this->redirect(array('action'=>'projects'));
 	    }
	    //send data to fields in view
	    $project = $this->Project->find('first', array('conditions'=>array('Project.project_id'=> $projectId)));
	    $this->set('id',$project['Project']['project_id']);
	    $this->set('desc',$project['Project']['description']);
    }

    //looks up one group entry, and all manager and employee entries to send to the view
    function manageGroup($groupId=null){
	    if($groupId==null){
		$this->redirect(array('action'=>'groups'));
 	    }
	    $group = $this->Group->find('first',array('conditions'=>array('Group.id'=>$groupId)));
	    $managers = $this->Manager->find('all');
	    $emps = $this->Employee->find('all');
	    //send data to view
	    $this->set('managers',$managers);
    	    $this->set('emps',$emps);
	    $this->set('groupManagers',$group['Managers']);
	    $this->set('groupMembers',$group['Employees']);
	    $this->set('groupId',$groupId);
    }

    //looks up one project entry, and all employee entries to send to the view
    function manageProject($projId=null){
	    if($projId==null){
		$this->redirect(array('action'=>'projects'));
 	    }
	    $project = $this->Project->find('first',array('conditions'=>array('Project.project_id'=>$projId)));
	    $emps = $this->Employee->find('all');
    	    $this->set('emps',$emps);
	    $this->set('projectMembers',$project['Employees']);
	    $this->set('projId',$projId);
    }

    //edit or delete an admin entry based on selected option
    function modifyAdmin(){
	$adminId =(int)$this->request->data['adminId'];
	//user selects delete
	if(((int)($this->request->data['adminAction']))==0){
		$this->Admin->delete($adminId,true);
		$this->redirect(array('action' => 'admins'));
	}
	//user selects edit
	else{
		$this->redirect(array('action'=>'editAdmin', $adminId));
	}
    }

    //edit or delete an employee entry based on selected option
    function modifyEmployee(){
	$empId = (int)($this->request->data['empId']);
	//user selects delete
	if(((int)($this->request->data['empAction']))==0){
		$this->Employee->delete($empId,true);
		$this->redirect(array('action' => 'employees'));
	}
	//user selects edit
	else{
		$this->redirect(array('action'=>'editEmployee', $empId));
	}
    }

    //edit or delete a group entry based on selected option
    function modifyGroup(){
	$groupId = (int)($this->request->data['groupId']);
	$action = (int)($this->request->data['action']);
	//user selects delete
	if($action==0){
		$this->Group->delete($groupId,true);
		$this->redirect(array('action' => 'groups'));
	}
	//user selects edit
	elseif($action==1){
		$this->redirect(array('action'=>'editGroup', $groupId));
	}
	//user selects add/remove groupmembers
	elseif($action==2){
		$this->redirect(array('action'=>'manageGroup', $groupId));
	}
    }

    //edit or delete a manager entry based on selected option
    function modifyManager(){
	$managerId = (int)($this->request->data['managerId']);
	//user selects delete
	if(((int)($this->request->data['managerAction']))==0){
		$this->Manager->delete($managerId,true);
		$this->redirect(array('action' => 'managers'));
	}
	//user selects edit
	else{
		$this->redirect(array('action'=>'editManager', $managerId));
	}
    }

    //edit or delete a project entry based on selected option
    function modifyProject(){
	$id = (int)($this->request->data['projectId']);
	$action = (int)($this->request->data['action']);
	//user selects delete
	if($action==0){
		$this->Project->delete($id,true);
		$this->redirect(array('action' => 'projects'));
	}
	//user selects edit
	elseif($action==1){
		$this->redirect(array('action'=>'editProject', $id));
	}
	//user selects add/remove project members
	elseif($action==2){
		$this->redirect(array('action'=>'manageProject', $id));
	}
    }

    //delete an entry from group_member or group_manager
    function removeFromGroup(){
	$groupId = (int)($this->request->data['groupId']);
	$id = (int)($this->request->data['deleteId']);
	$empCount = $this->GroupMember->find('count',array('conditions'=>array('GroupMember.emp_id'=>$id)));
	$mgrCount = $this->GroupManager->find('count',array('conditions'=>array('GroupManager.mgr_id'=>$id)));
	//if the id received from the view is an employee, delete the entry from group_member
	if($empCount>0){
		try{
			$grpMember = $this->GroupMember->find('first',array('conditions'=>array('GroupMember.group_id'=>$groupId,'GroupMember.emp_id'=>$id)));
			$this->GroupMember->delete($grpMember['GroupMember']['id'],false);
		}
		catch(Exception $e){
		}
	}
	//if the id received from the view is a manager, delete the entry from group_manager
	if($mgrCount>0){
		try{
			$grpManager = $this->GroupManager->find('first',array('conditions'=>array('GroupManager.group_id'=>$groupId,'GroupManager.mgr_id'=>$id)));
			$this->GroupManager->delete($grpManager['GroupManager']['id'],false);
		}
		catch(Exception $e){
		}
	}
	$this->redirect(array('action'=>'manageGroup',$groupId));
    }

    //delete an entry from projectMembers
    function removeFromProject(){
	$projId = (int)($this->request->data['projId']);
	$id = (int)($this->request->data['deleteId']);
	try{
		$prjMember = $this->ProjectMember->find('first',array('conditions'=>array('ProjectMember.project_id'=>$projId,'ProjectMember.employee_id'=>$id)));
		$this->ProjectMember->delete($prjMember['ProjectMember']['id'],false);
	}
	catch(Exception $e){
		
    	}
	$this->redirect(array('action'=>'manageProject',$projId));
    }

    //modify an admin entry using data received from the view
    function updateAdmin($adminId=null){
	if($adminId==null){
		$this->redirect(array('action'=>'admins'));
 	}
	$name = $this->request->data['admin_name'];
	$admin = $this->Admin->find('first',array('conditions'=>array('Admin.admin_id'=>$adminId)));
	$admin['Admin']['admin_name']=$name;
	try{
		$this->Admin->Save($admin);
	}
	catch(Exception $e){
	}
	$this->redirect(array('action'=>'admins'));
    }
    
    //modify an employee entry using data received from the view
    function updateEmployee($empId=null){
	if($empId==null){
		$this->redirect(array('action'=>'employees'));
 	}
	$name = $this->request->data['employee_name'];
	$emp = $this->Employee->find('first',array('conditions'=>array('Employee.employee_id'=>$empId)));
	$emp['Employee']['name']=$name;
	try{
		$this->Employee->Save($emp);
	}
	catch(Exception $e){
	}
	$this->redirect(array('action'=>'employees'));
    }

    //modify a group entry using data received from the view
    function updateGroup($groupId=null){
	if($groupId==null){
		$this->redirect(array('action'=>'groups'));
 	}
	$name = $this->request->data['name'];
	$desc = $this->request->data['description'];
	$group = $this->Group->find('first',array('conditions'=>array('Group.id'=>$groupId)));
	$group['Group']['name']=$name;
	try{
		$this->Group->Save($group);
	}
	catch(Exception $e){
	}
	$this->redirect(array('action'=>'groups'));
    }

    //modify a manager entry using data received from the view
    function updateManager($managerId=null){
	if($managerId==null){
		$this->redirect(array('action'=>'managers'));
 	}
	$name = $this->request->data['manager_name'];
	$man = $this->Manager->find('first',array('conditions'=>array('Manager.manager_id'=>$managerId)));
	$man['Manager']['name']=$name;
	try{
		$this->Manager->Save($man);
	}
	catch(Exception $e){
	}
	$this->redirect(array('action'=>'managers'));
    }

     //modify a project entry using data received from the view
     function updateProject($projId=null){
	if($projId==null){
		$this->redirect(array('action'=>'projects'));
 	}
	$desc = $this->request->data['description'];
	$proj = $this->Project->find('first',array('conditions'=>array('Project.project_id'=>$projId)));
	$proj['Project']['description']=$desc;
	try{
		$this->Project->Save($proj);
	}
	catch(Exception $e){
	}
	$this->redirect(array('action'=>'projects'));
    }

    


    
    

    
}
?>
